package com.wwz.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisUtils {
    @Value("${spring.redis.sentinel.nodes}")
    private String redisNodes;
    @Value("${spring.redis.sentinel.master}")
    private String master;
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(){
        RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
        String[] host = redisNodes.split(",");
        for(String redisHost : host){
            String[] item = redisHost.split(":");
            String ip = item[0];
            String port = item[1];
            configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
        }
        configuration.setMaster(master);
        configuration.setPassword("root123456abc");
        return configuration;
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.master.jedis.pool")
    public JedisPoolConfig config(){
        return new JedisPoolConfig();
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.master")
    public JedisConnectionFactory redisConnectionFactory(){
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(),config());
        return redisConnectionFactory;
    }
    @Bean("masterRedisTemplate")
    public RedisTemplate masterRedisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }

    @Bean("masterStringRedisTemplate")
    public StringRedisTemplate masterStringRedisTemplate(){
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }



    /*@Bean
    @ConfigurationProperties(prefix = "spring.redis.slave")
    public JedisConnectionFactory slaveRedisConnectionFactory(){
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
        redisConnectionFactory.setPoolConfig(config());
        return redisConnectionFactory;
    }
    @Bean("slaveRedisTemplate")
    public RedisTemplate slaveRedisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(slaveRedisConnectionFactory());
        return redisTemplate;
    }

    @Bean("slaveStringRedisTemplate")
    public StringRedisTemplate slaveStringRedisTemplate(){
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(slaveRedisConnectionFactory());
        return redisTemplate;
    }*/
}
